#include <iostream>
#include <unistd.h>
#include <cassert>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
using namespace std;

int main()
{
    int fds[2];
    int n=pipe(fds);
    assert(n == 0);
    pid_t id =fork();
    assert(id>=0);
    if(id == 0)
    {
        close(fds[0]);
        //子进程
        int cnt=0;
        const char *s= "我是子进程，我在给父进程发消息";
        while(true)
        {
        cnt++;
        char buffer[1024];
        snprintf(buffer,sizeof buffer,"child -> parent say: %s[%d][%d]",s,cnt,getpid());
        ssize_t ss=write(fds[1],buffer,strlen(buffer));
        sleep(1);
        // break;
        }
        //close(fds[1]);
        exit(0);
    }
    close(fds[1]);
    while(true)
    {
        char buffer[1024];
        ssize_t ss = read(fds[0],buffer,sizeof(buffer)-1);
        if(ss>0)buffer[ss]= 0;
        cout<<"Get Message"<<buffer<<"| mypid: "<<getpid()<<endl;
        break;
    }
    close(fds[0]);
    int status=0;
    n = waitpid(id,&status,0);
    assert(n == id);
    cout<<"pid->"<< n << ":" << (status & 0x7f) <<endl;
    return 0;
}
